<?php
/**
 * General template tags that are used throughout a template.
 *
 * @package WP_Framework
 */

/* @group START: Framework General */

/**
 * get_theme_functions() - include theme.php from theme assets dir if file exist
 *
 * @since - 0.3
 * @needsdoc
 */
function get_theme_functions() {
	if ( file_exists( THEMEASSETS . '/theme.php' ) ) include_once( THEMEASSETS . '/theme.php' );
}

/**
 * wpframework_credits() - echos credits link
 *
 * @since - 0.1
 * @needsdoc
 * @filter wpframework_credits
 */
function wpframework_credits( $sep = ' &mdash; ', $echo = false ) {
	$credits = 'Built on the <a rel="developer" title="WordPress Theme Framework" href="http://wpframework.com">WP Framework</a>'. $sep .'Powered by <a href="http://wordpress.org">WordPress</a>';
	$credits = apply_filters( 'wpframework_credits', $credits ); // Available filter: wpframework_credits
	return ( $echo ) ? $credits : print( $credits );
}

/**
 * wpframework_version() - returns WP Framework's version defined in style.css
 *
 * @since - 0.3
 * @needsdoc
 */
function wpframework_version(){
	$theme_data = implode( '', file( TEMPLATEPATH . '/style.css' ) );
	$theme_data = str_replace ( '\r', '\n', $theme_data );	
	preg_match( '/\$WP_Framework:\t(\d.\d|\d.\d.\d) \$/', $theme_data, $matches );

	return apply_filters( 'wpframework_version', $matches[1] ); // Available filter: wpframework_credits
}

/**
 * framework_doctype - choose which XHTML doctype you need
 *
 * @since 0.3
 * @needsdoc
 * @filter framework_doctype
 */
function framework_doctype( $dt = 'xhtml-trans', $echo = true ) {
	do_action( 'framework_pageload' ); // Available hook: framework_pageload
	
	switch ( $dt ) {
		case 'xhtml-trans':
			$doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
			break;
		case 'xhtml-strict':
			$doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
			break;
		case 'xhtml-1.1':
			$doctype = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n";
			break;
	}
	$doctype = apply_filters( 'framework_doctype', (string) $doctype ); // Available filter: framework_doctype
	return ( $echo ) ? print( $doctype ) : $doctype;
}

/**
 * framework_head - includes all theme meta in the <head> tag
 *
 * @since 0.3
 * @needsdoc
 * @filter framework_head_meta, framework_head_favicons, framework_head_stylesheets, framework_head
 * @uses framework_hook_head
 */
function framework_head( $echo = true, $meta = NULL, $stylesheets = NULL, $feed = NULL ) {
	if ( apply_filters( 'include_meta', (bool) true ) ) { // Available filter: include_meta
		$meta  = "\n<!-- Meta Tags -->\n";
		$meta .= "<meta http-equiv=\"Content-Type\" content=\"". get_bloginfo( 'html_type' ) ."; charset=". get_bloginfo('charset') ."\" />\n";
		$meta .= "<meta name=\"generator\" content=\"". framework_version( 'wp' ) ."\" />\n";
		$meta .= "<meta name=\"template\" content=\"". framework_version( 'wpf' ) ."\" />\n";
		$meta .= apply_filters( 'framework_head_meta', (string) '' ); // Available filter: framework_head_meta
	}
	
	if ( apply_filters( 'include_css', (bool) true ) ) { // Available filter: include_css
		$stylesheets  = "\n<!-- Stylesheets -->\n";
		$stylesheets .= "<link rel=\"stylesheet\" href=\"". get_bloginfo( 'stylesheet_url' ) ."\" type=\"text/css\" media=\"screen\" />\n";
		$stylesheets .= apply_filters( 'framework_head_css', (string) '' ); // Available filter: framework_head_css
	}
	
	if ( apply_filters( 'include_feed', (bool) true ) ) { // Available filter: include_feed
		$feed  = "\n<!-- RSS and Atom Syndication -->\n";
		// Use feed_links() and feed_links_extra() in the futre. (needs to be backwards compat.) Also check out automatic_feed_links();
		$feed .= "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"". get_bloginfo( 'name' ) ." RSS Feed\" href=\"". get_bloginfo( 'rss2_url' )."\" />\n";
		$feed .= "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"". get_bloginfo( 'name' ) ." Atom Feed\" href=\"". get_bloginfo( 'atom_url' )."\" />\n\n";
		$feed .= "<!-- Pingback -->\n";
		$feed .= "<link rel=\"pingback\" href=\"". get_bloginfo( 'pingback_url' ) ."\" />\n";
	}
	
	// All together now
	$head = $meta . $stylesheets . $feed;
	echo apply_filters( 'framework_head', (string) $head ); // Available filter: framework_head
	//return ( $echo ) ? print( $framework_head ) : $framework_head; // why aren't you working?
}

/**
 * comment_reply_js - loads the javascript required for threaded comments
 *
 * @since 0.3
 * @needsdoc
 * @uses framework_hook_head
 */
function comment_reply_js() {
	if ( !is_singular() ) return;
	if ( get_option( 'thread_comments' ) ) wp_enqueue_script( 'comment-reply' );
}

/**
 * framework_version() - Gives you the ability to hide or show WordPress/WP Framework version. (hides by default)
 *
 * @since - 0.3
 * @dontlike
 * @needsdoc
 * @todo refactor
 * @filter framework_get_version
 * @see framework.php
 */
function framework_version( $show, $echo = false ) {
	$hide = apply_filters( 'framework_version', (bool) true ); // Available filter: framework_version
	$version = NULL;
	if ( $hide ) {
		switch ( $show ) {
			case 'wp':
				$version = 'WordPress';
				break;
			case 'wpf':
				$version = 'WP Framework';
				break;
		}
	} else {
		switch ( $show ) {
			case 'wp':
				$version = 'WordPress ' . get_bloginfo( 'version' );
				break;
			case 'wpf':
				$version = 'WP Framework ' . wpframework_version();
				break;
		}
	}
	return ( $echo ) ? print( $version ) : $version;
}

/**
 * framework_title() - Generates semantic classes for the <title> tag with extra SEO love.
 *
 * @todo refactor code
 * @since - 0.2
 * @filter semantic_title
 */
function framework_title( $sep = '&mdash;' ) {
	if ( is_single() ) : wp_title( '&raquo;', true, 'right' ); bloginfo( 'name' );
	echo ( ' - ' );
	echo bloginfo( 'description' );
	
	elseif ( is_page() || is_paged() ) : wp_title( '&raquo;', true, 'right' ); 
	bloginfo( 'name' ); echo ( ' - ' );
	echo bloginfo( 'description' );
	
	elseif ( is_author() ) : wp_title( 'Archives for ', true, 'left' );
	echo ( ' &raquo; ' ); bloginfo( 'name' );
	echo ( ' - ' );
	echo bloginfo( 'description' );
	  
	elseif ( is_archive() ) : wp_title( 'Archives for ', true, 'left' );
	echo ( ' &raquo; ' ); bloginfo( 'name' );
	echo ( ' - ' );
	echo bloginfo( 'description' );
	
	elseif ( is_search() ) : wp_title('Search Results ', true, 'left' );
	echo ( ' &raquo; ' ); bloginfo( 'name' );
	echo ( ' - ' );
	echo bloginfo( 'description' );
	
	elseif ( is_404() ) : wp_title( '404 Error Page Not Found ', true, 'left' );
	echo ( ' &raquo; ' ); bloginfo( 'name' );
	echo ( ' - ');
	echo bloginfo( 'description' );
	
	else : wp_title( '&raquo', true, 'left' ); bloginfo( 'name' );
	echo ( ' - ' );
	echo bloginfo( 'description' );         
	endif;
}

/**
 * framework_menu - placeholder for framework's menu class
 *
 * @since 0.3
 * @needsdoc
 * @todo add more functionality (parameters, menu class, everything)
 * @uses framework_hook_menu
 * @see framework.php
 */
function framework_menu( $echo = true, $args = array() ) {
	$framework_menu = wp_page_menu( $args );
	
	return ( $echo ) ? print( $framework_menu ) : $framework_menu;
}

/**
 * framework_menu - adds css class to the <ul> tag in wp_page_menu.
 *
 * @since 0.3
 * @filter framework_menu_ulclass
 * @needsdoc
 */
function framework_menu_ulclass( $ulclass ) {
	$classes = apply_filters( 'framework_menu_ulclass', (string) 'nav' ); // Available filter: framework_menu_ulclass
	return preg_replace( '/<ul>/', '<ul class="'. $classes .'">', $ulclass, 1 );
}

/**
 * framework_searchform - search form. What else is there to say?
 *
 * @since 0.3
 * @needsdoc
 * @todo work on parameters
 * @filter framework_search_title, framework_search_txt, framework_searchform
 */
function framework_searchform( $echo = true, $search_title = 'Search', $search_txt = 'To search, type and hit enter' ) {
	$search_title = apply_filters( 'framework_search_title', $search_title ); // Available Filter: framework_search_title
	$search_txt = apply_filters( 'framework_search_txt', $search_txt ); // Available Filter: framework_search_txt

	$searchform  = do_action( 'framework_before_searchform' ); // Available hook: framework_before_searchform

	$searchform .= '<!--BEGIN #searchform-->' . "\n";
	$searchform .= '<form class="searchform" method="get" action="';	
	$searchform .= get_bloginfo( 'url' ) . '">' . "\n";
	$searchform .= do_action( 'framework_searchform_open' ); // Available hook: framework_searchform_open

	$searchform .= '<input class="search-txt" name="s" type="text" value="'. $search_txt. '" onfocus="if (this.value == \''. $search_txt. '\') {this.value = \'\';}" ';
	$searchform .= 'onblur="if (this.value == \'\') {this.value = \''. $search_txt .'\';}" />' . "\n";
	$searchform .= '<input type="hidden" class="searchsubmit" value="'. $search_title .'" />' . "\n";

	$searchform .= do_action( 'framework_searchform_close' ); // Available hook: framework_searchform_close
	$searchform .= '<!--END #searchform-->' . "\n";
	$searchform .= '</form>' . "\n";

	$searchform .= do_action( 'framework_after_searchform' ); // Available hook: framework_after_searchform

	$search = apply_filters( 'framework_searchform', (string) $searchform ); // Available Filter: framework_searchform
	return ( $echo ) ? print( $search ) : $search;	
}

/**
 * framework_get - Gets template files
 *
 * @since 0.3
 * @needsdoc
 * @action framework_get
 * @todo test this on child themes
 */
function framework_get( $file = NULL ) {
	do_action( 'framework_get' ); // Available hook: framework_get
	$error = "Sorry, but <strong><code>{$file}</code></strong> does <strong><em>not</em></strong> exist. Please make sure this file exist in <strong>" . get_stylesheet_directory() . "</strong>";
	$error = apply_filters( 'framework_get_error', (string) $error ); // Available filter: framework_get_error
	if ( isset( $file ) && file_exists( get_stylesheet_directory() . "/{$file}.php" ) )
		locate_template( get_stylesheet_directory() . "/{$file}.php" );
	else
        echo $error;
}

/**
 * framework_page_title - displays the correct page title based on requested template.
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimizedm, refactored. Make is_search() cooler. go filter crazy?
 * @filter framework_page_title
 */
function framework_page_title( $echo = true ) {
	global $wpdb, $wp_locale, $wp_query, $s;
	$page_title = NULL;
	$wp_title = trim( wp_title('','') );
	
	if ( is_search() ) {
		$page_title  = "<h1 class=\"page-title search-title\">Search Results for: <span class=\"search-term\">$s</span></h1>\n";
	
	} elseif ( is_404() ) {
		$page_title  = "<h1 class=\"page-title\">404 Error, Not found</h1>\n";
		
	} elseif ( is_archive() ) {
	
		if ( is_date() ) {
			if ( is_day() ) {
				if ( get_the_time( 'F jS, Y' ) )
					$page_title = '<h1 class="page-title">Daily archives: <span class="archive-title">'. get_the_time( 'F jS, Y' ) .'</span></h1>';
				else
					$page_title = '<h1 class="page-title">Daily archives</h1>';
			
			} elseif ( is_month() ) {
				if ( get_the_time( 'F, Y' ) )
					$page_title = '<h1 class="page-title">Monthly archives: <span class="archive-title">'. get_the_time( 'F, Y' ) .'</span></h1>';
				else
					$page_title = '<h1 class="page-title">Monthly archives</h1>';
			
			} elseif ( is_year() ) {
				if ( get_the_time( 'Y' ) )
					$page_title = '<h1 class="page-title">Yearly archives: <span class="archive-title">'. get_the_time( 'Y' ) .'</span></h1>';
				else
					$page_title = '<h1 class="page-title">Yearly archives</h1>';
			}
		
		} elseif ( is_author() ) {
			if ( strlen( $wp_title ) > 1 )
				$page_title = '<h1 class="page-title">Author archives: <span id="archive-title">'. $wp_title .'</span></h1>';
			else
				$page_title = '<h1 class="page-title">Author archives</h1>';
		
		} elseif ( is_category() && !is_tag() ) {
			if ( strlen( $wp_title ) > 1 )
				$page_title = '<h1 class="page-title">Category archives: <span id="archive-title">'. $wp_title .'</span></h1>';
			else
				$page_title = '<h1 class="page-title">Category archives</h1>';
		
		} elseif ( is_tag() && !is_category() ) {
			if ( strlen( $wp_title ) > 1 )
				$page_title = '<h1 class="page-title">Entries Tagged: <span id="archive-title">'. $wp_title .'</span></h1>';
			else
				$page_title = '<h1 class="page-title">Tag archives</h1>';
		
		} else {
			$page_title = '<h1 class="page-title">Archives</h1>';
		}
	} // is_archive()
	
	$page_title = apply_filters( 'framework_page_title', (string) $page_title ); // Available filter: framework_page_title
	
	// And tada!
	echo $page_title;
	//return ( $echo ) ? print( $page_title ) : $page_title;
}

/**
 * framework_after_loop - acts as a wrapper for anything going in framework_hook_after_loop()
 *
 * @since 0.3
 */
function framework_after_loop() {
	// single.php, page.php, attachment.php
	if ( is_singular() ) {
		framework_page_navigation();
		comments_template( '', true );
		framework_trackback_rdf();
	}
	// index.php, archive.php, author.php, category.php, tag.php, date.php, search.php BUT NOT page.php, single.php, attachment.php
	elseif ( is_front_page() || is_archive() || is_search() && !is_singular() ) {
		framework_page_navigation();
	}
}

/**
 * framework_trackback_rdf - displays RDF Trackback information within an HTML comment
 *
 * @since 0.3
 * @needsdoc
 */
function framework_trackback_rdf(){
	echo "<!--START trackback_rdf \n";
	trackback_rdf();
	echo "\nEND trackback_rdf-->\n";
}

/**
 * framework_page_navigation - smart page navigation
 *
 * @since 0.3
 * @uses framework_hook_page_navigation
 * @todo make nav-previous/next easily filterable/hookable/extendable.
 */
function framework_page_navigation() {
	do_action( 'framework_hook_page_navigation' ); // Available hook: framework_hook_page_navigation
	
	global $paged, $wp_query;
	$maxpage = intval( $wp_query->max_num_pages );
	$page = intval( $paged );
	
	$next = get_adjacent_post( false, '', false);
	$prev = get_adjacent_post( false, '');
	
	if ( is_singular() ) { ?>
	<!--BEGIN .navigation-links-->
	<div class="navigation-links single-page-navigation">
        <?php if ( $prev ) { ?>
        <div class="nav-previous"><?php previous_post_link( '&laquo; <span class="nav-meta">%link</span>' ); ?></div>
        <?php } if ( $next ) { ?>
        <div class="nav-next"><?php next_post_link('%link <span class="nav-meta">&raquo;</span>'); ?></div>
        <?php } ?>
	<!--END .navigation-links-->
	</div>
	<?php } elseif ( $maxpage < 1 ) { ?>
	<!--BEGIN .navigation-links-->
	<div class="navigation-links page-navigation">
    	<?php if ( $page == 0 && $maxpage > $page ) { // go back in time ?>
        <span class="nav-next"><?php next_posts_link( '<span class="nav-meta">&laquo;</span> Older Entries' ); ?></span>
        <?php } if ( $page > 1 ) { // go to the front page ?>
        <span class="nav-previous"><?php previous_posts_link( 'Newer Entries <span class="nav-meta">&raquo;</span>' ); ?></span>
        <?php } ?>
	<!--END .navigation-links-->
	</div>
	<?php }
}

/* @group END: Framework General */
/* @group START: Framework CSS Class Functions */

/**
 * framework_body_class - css classes for <body> tag
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_body_class
 * @test
 */
function framework_body_class( $echo = true ) {
	$classes = get_body_class(); // Use WordPress core function by default

	// Separates classes with a single space, collates classes for the <body> tag
	$classes = join( ' ', apply_filters( 'framework_body_class', (array) $classes ) ); // Available filter: framework_body_class
	
	// And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_container_class - css classes for the container div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_container_class
 */
function framework_container_class( $echo = true ) {
	$classes = array( 'container' );
	
	// Separates classes with a single space, collates classes for the .container tag
    $classes = join( ' ', apply_filters( 'framework_container_class', (array) $classes ) ); // Available filter: framework_container_class

    // And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_header_class - css classes for the header div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_header_class
 */
function framework_header_class( $echo = true ) {
	$classes = array( 'header' );
	
	// Separates classes with a single space, collates classes for the .header tag
    $classes = join( ' ', apply_filters( 'framework_header_class', (array) $classes ) ); // Available filter: framework_header_class

    // And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_content_class - css classes for the content div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_content_class
 */
function framework_content_class( $echo = true ) {
	$classes = array( 'content' );
	
	// Separates classes with a single space, collates classes for the .content tag
    $classes = join( ' ', apply_filters( 'framework_content_class', (array) $classes ) ); // Available filter: framework_content_class

    // And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_hfeed_class - css classes for the hfeed div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_hfeed_class
 */
function framework_hfeed_class( $echo = true ) {
	$classes = array( 'hfeed' );
	
	// Separates classes with a single space, collates classes for the .hfeed tag
    $classes = join( ' ', apply_filters( 'framework_hfeed_class', (array) $classes ) ); // Available filter: framework_hfeed_class

    // And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_entry_class - adds post metadata before the entry_content div
 *
 * @since 0.3
 * @test
 */
function framework_entry_class( $echo = true ){
	$classes = get_post_class(); // Use WordPress core function by default
	
	// Separates classes with a single space, collates classes for the <body> tag
	$classes = join( ' ', apply_filters( 'framework_entry_class', (array) $classes ) ); // Available filter: framework_entry_class
	
	// And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_sidebar_class - css classes for the aside div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_sidebar_class
 */
function framework_sidebar_class( $echo = true ) {
	$classes = array( 'aside' );
	
	// Separates classes with a single space, collates classes for the .aside tag
    $classes = join( ' ', apply_filters( 'framework_sidebar_class', (array) $classes ) ); // Available filter: framework_sidebar_class

    // And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_comments_class - css classes for the #comments div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_comments_class
 */
function framework_comments_class( $echo = true ) {
	$classes = array();
	
	// Separates classes with a single space, collates classes for the #comments tag
    $classes = join( ' ', apply_filters( 'framework_comments_class', (array) $classes ) ); // Available filter: framework_comments_class
	if ( $classes )
		$output = 'class="'. $classes .'"';
	else
		$output = NULL;
    // And tada!
    return $echo ? print( $output ) : $output;
}

/**
 * framework_comment_class - css classes for each individual comment
 *
 * @since 0.3
 * @alot more
 */
function framework_comment_class( $echo = true ){
	$classes = get_comment_class(); // Use WordPress core function by default
	
	// Separates classes with a single space, collates classes for the <li> tag
	$classes = join( ' ', apply_filters( 'framework_comment_class', (array) $classes ) ); // Available filter: framework_comment_class
	
	// And tada!
    return $echo ? print( $classes ) : $classes;
}

/**
 * framework_respond_class - css classes for the #respond div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_respond_class
 */
function framework_respond_class( $echo = true ) {
	$classes = array();
	
	// Separates classes with a single space, collates classes for BODY
    $classes = join( ' ', apply_filters( 'framework_respond_class', (array) $classes ) ); // Available filter: framework_respond_class
	if ( $classes )
		$output = 'class="'. $classes .'"';
	else
		$output = NULL;
    // And tada!
    return $echo ? print( $output ) : $output;
}

/**
 * framework_footer_class - css classes for the footer div
 *
 * @since 0.3
 * @needsdoc
 * @todo needs to be optimized
 * @filter framework_footer_class
 */
function framework_footer_class( $echo = true ) {
	$classes = array( 'footer' );
	
	// Separates classes with a single space, collates classes for BODY
    $classes = join( ' ', apply_filters( 'framework_footer_class', (array) $classes ) ); // Available filter: framework_footer_class

    // And tada!
    return $echo ? print( $classes ) : $classes;
}

/* @group END: Framework CSS Class Functions */
?>